package com.laikan.reader.bookfriend.activity;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.helpers.BasicEvent;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.requester.SigninRequester;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.adapter.ShareReadRuleAdapter;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.SignInBean;
import com.laikan.reader.core.bean.SignInSuccessBean;
import com.laikan.reader.core.events.LaiKanEvent;
import com.laikan.reader.core.utils.PublicUtil;
import com.laikan.reader.core.view.DownBoundScrollView;
import com.laikan.reader.core.view.MyListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wx
 * @date 2016/3/7:10:38
 * @description 签到页面
 */
public class SignInActivity extends BasicActivity implements View.OnClickListener {

    private String[] mSignInRule;//获取签到规则
    private MyListView sign_in_rule_listview;
    private ShareReadRuleAdapter mRuleAdapter;
    private BaseDataBean<SignInBean> result;
    private BaseDataBean<SignInSuccessBean> mSignInRequestResult;

    private DownBoundScrollView downScrollview;//自定义下拉scrollview
    private RelativeLayout rl_title, signin_rootview; //头部显示 “签到有好礼”
    private LinearLayout sign_rule;
    private SignInPopupWindow signInPopupWindow;//签到页面
    private ImageView iv_first_day, iv_second_day, iv_third_day, iv_fourth_day, iv_fiveth_day;//五天按钮
    private ImageView  sign_img_bg, iv_back;
    private TextView sign_in_text, tv_title, sign_in_record;
    private List<ImageView> imageViews = new ArrayList<ImageView>();
    private View top_divider;
    Drawable drawable_back_tv;
    private Animation anim;//获取动画

    private int addSignCount = 0;
    private int todayWeek = 0;//当前第几天

    @Override
    protected void assignViews() {
        setContentView(R.layout.activity_signin);
        signin_rootview = (RelativeLayout)findViewById(R.id.signin_rootview);
        rl_title = (RelativeLayout)findViewById(R.id.rl_title);
        iv_back = (ImageView)findViewById(R.id.iv_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        top_divider = findViewById(R.id.top_divider);

        downScrollview = (DownBoundScrollView)findViewById(R.id.downScrollview);
        sign_in_rule_listview = (MyListView)findViewById(R.id.sign_in_rule_listview);

        sign_img_bg = (ImageView)findViewById(R.id.sign_img_bg);
        sign_in_record = (TextView)findViewById(R.id.sign_in_record);
        sign_in_text = (TextView)findViewById(R.id.sign_in_text);

        //七天按钮
        iv_first_day = (ImageView)findViewById(R.id.iv_first_day);
        iv_second_day = (ImageView)findViewById(R.id.iv_second_day);
        iv_third_day = (ImageView)findViewById(R.id.iv_third_day);
        iv_fourth_day = (ImageView)findViewById(R.id.iv_fourth_day);
        iv_fiveth_day = (ImageView)findViewById(R.id.iv_fiveth_day);
        imageViews.add(iv_first_day);
        imageViews.add(iv_second_day);
        imageViews.add(iv_third_day);
        imageViews.add(iv_fourth_day);
        imageViews.add(iv_fiveth_day);

        sign_rule = (LinearLayout)findViewById(R.id.sign_rule);
    }

    @Override
    protected void initData() {
        openEventBus();
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        scrollHeadBg();//设置scroll滚动监听

        mSignInRule = getResources().getStringArray(R.array.signInRule);
        mRuleAdapter = new ShareReadRuleAdapter(mContext);
        mRuleAdapter.setDatas(new ArrayList<String>(Arrays.asList(mSignInRule)));
        sign_in_rule_listview.setDivider(null);
        sign_in_rule_listview.setAdapter(mRuleAdapter);

        anim = AnimationUtils.loadAnimation(mContext, R.anim.myscale);

        //签到成功提示
        signInPopupWindow = new SignInPopupWindow(mContext);
        //获取签到数据
        SigninRequester.requestData(this, SigninRequester.SIGNIN_REQUEST_SIGNPAGE);
    }

    @Override
    protected void setListener() {
        for (int i = 0; i < imageViews.size(); i++) {
            imageViews.get(i).setOnClickListener(this);
        }
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SPUtil.putBoolean("signedAction", false);
    }

    /**
     * 设置头部滚动监听
     */
    private void scrollHeadBg() {
        final int maxOffset = AndroidUtils.dip2px(this, 70.0f);
        final int onsetOffset = AndroidUtils.dip2px(this, 70.0f);

        downScrollview.setOnScrollListener(new DownBoundScrollView.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                int color = 0;
                if (scrollY < onsetOffset) {
                    color = Color.argb(0, 0, 0, 0);
                    rl_title.setBackgroundColor(color);
                    top_divider.setVisibility(View.GONE);
                    tv_title.setTextColor(Color.parseColor("#FFFFFF"));
                    iv_back.setImageResource(R.drawable.lk_back_small);
                }else {
                    color = Color.argb(255, 255, 255, 255);
                    rl_title.setBackgroundColor(color);
                    top_divider.setVisibility(View.VISIBLE);
                    tv_title.setTextColor(Color.parseColor("#333333"));
                    iv_back.setImageDrawable(drawable_back_tv);
                }
            }

            @Override
            public void stopTimer() {
            }

            @Override
            public void startTimer() {
            }
        });

    }

    /**
     * 设置签到页面
     */
    private void setView(BaseDataBean<SignInBean> result) {
        sign_img_bg.setOnClickListener(this);
        SignInBean signInResult = result.getData();
        //设置一周每天的签到状态
        for (int i = 0; i < signInResult.getWeekSigninList().size() - 2; i++) {
            if (signInResult.getWeekSigninList().get(i)) {
                imageViews.get(i).setImageResource(R.drawable.signed);
            }else {
                imageViews.get(i).setImageResource(R.drawable.un_sign);
            }
        }
        //获取当前是星期几
        todayWeek = Integer.parseInt(signInResult.getTodayWeek());

        int mCurrentDay = todayWeek - 1;
        if (signInResult.getWeekSigninList().get(mCurrentDay)) {
            imageViews.get(mCurrentDay).setImageResource(R.drawable.signed);
            sign_in_text.setText(R.string.signin_signed_text);
            sign_img_bg.setOnClickListener(null);
            }else {
            imageViews.get(mCurrentDay).setImageResource(R.drawable.un_sign);
            sign_in_text.setText(R.string.signin_unsignin_text);
            }
        sign_rule.setVisibility(View.VISIBLE);

        if ("".equals(signInResult.getTotalSigninCount()) || null == signInResult.getTotalSigninCount()) {
            addSignCount = 0;
        }else {
            addSignCount = Integer.parseInt(signInResult.getTotalSigninCount());
        }
        sign_in_record.setText(DeviceUtil.getHtmlFormatedString(SignInActivity.this, R.string.signin_total_day, addSignCount));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.sign_img_bg:
                sign_img_bg.startAnimation(anim);
                if (todayWeek >= 1 && !result.getData().getWeekSigninList().get(todayWeek - 1)) {
                    SigninRequester.signInRequest(imageViews.get(todayWeek - 1), todayWeek, 0);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void parseEvent(BasicEvent event) {
        super.parseEvent(event);
        switch (event.getEventID()) {
            case LaiKanEvent.EVENT_ID_SIGNIN_DATAREQUEST:
                result = (BaseDataBean<SignInBean>)event.getElves();
                if (result != null) {
                    setView(result);
                }
                break;
            case LaiKanEvent.EVENT_ID_SIGNIN_SIGNREQUEST:
                mSignInRequestResult = (BaseDataBean<SignInSuccessBean>)event.getElves();
                if (mSignInRequestResult != null) {
                    sign_img_bg.setOnClickListener(null);
                    signInPopupWindow.setContentView("66", mSignInRequestResult.getData().getSigninTicket() + "+" + mSignInRequestResult.getData().getSigninGrowthVal());
                    signInPopupWindow.showAtLocation(signin_rootview, Gravity.CENTER, 0, 0);

                    sign_in_text.setText(R.string.signin_signed_text);
                    sign_in_record.setText(DeviceUtil.getHtmlFormatedString(SignInActivity.this, R.string.signin_total_day, addSignCount + 1));
                    int mCurrentDay = Integer.parseInt(result.getData().getTodayWeek()) - 1;
                    imageViews.get(mCurrentDay).setImageResource(R.drawable.signed);

                    //关闭书架签到图标显示
                    LaiKanEvent laiKanEvent = LaiKanEvent.getInstance();
                    laiKanEvent.setEventID(LaiKanEvent.EVENT_ID_BOOKSHELF_CLOSE_SIGNIN);
                    LaikanApplication.postEvent(laiKanEvent);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
